package com.lengyue.imakiba.show;

import java.util.List;


import com.jfinal.core.Controller;
import com.lengyue.imakiba.data.Anime;
import com.lengyue.imakiba.data.AnimeInterface;
import com.lengyue.imakiba.data.Type;

public class Index extends Controller implements AnimeInterface {
	public void index()
	{
		//导航栏动漫类型
		List<Type> types = Type.dao.find("select * from type order by sort_no");
		//连载动漫
		//List<Anime> serializeAnimes = getAnimeByState("serialize", 15);
		//最新动漫
		List<Anime> recentAnimes = getAnimeByState(null, 15);
		//完结动漫
		List<Anime> endAnimes = getAnimeByState("end", 15);
		//动漫预告
		List<Anime> pvAnimes = getAnimeByState("pv", 15);
		setAttr("types", types);
		//setAttr("serializeAnimes",serializeAnimes);
		setAttr("recentAnimes",recentAnimes);
		setAttr("endAnimes",endAnimes);
		setAttr("pvAnimes",pvAnimes);
		setAttr("coverPath", coverPath);
		render("/WEB-INF/freeMarkerTemplete/index.ftl");
	}
	private List<Anime> getAnimeByState(String state,int num)
	{
		String sql = "select DISTINCT a.* from anime a left join episode e on e.anime_id = a.id";
		if(state!=null)
		{
			sql += " where a.state = ?";
			return Anime.dao.find(sql+" order by date desc limit ?",state,num);
		}else
		{
			return Anime.dao.find(sql+" order by date desc limit ?",num);
		}
	}
}
